package cc.rengu.redp.bizimpl.csmc.service.impl;

import cc.rengu.redp.bizimpl.csmc.dao.CupsBrandFeeReportMapper;
import cc.rengu.redp.bizimpl.csmc.domain.CupsBrandFeeReportInfo;
import cc.rengu.redp.bizimpl.csmc.service.CupsBrandFeeReportService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.utils.SortUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
@DS("csmc")
public class CupsBrandFeeReportServiceImpl extends ServiceImpl<CupsBrandFeeReportMapper,CupsBrandFeeReportInfo> implements CupsBrandFeeReportService {
    @Autowired
    private IUpmpSysParamService upmpSysParamService;

    @Autowired
    private CupsBrandFeeReportMapper cupsBrandFeeReportMapper;

    @Override
    public IPage<CupsBrandFeeReportInfo> findByPage(QueryRequest request, CupsBrandFeeReportInfo entity) {
        try {
                LambdaQueryWrapper<CupsBrandFeeReportInfo> queryWrapper = new LambdaQueryWrapper<>();
                UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
                String instId = "";
                if (null != user && user.isAdmin()) {
                    instId = entity.getInstId();
                    if (StringUtils.isBlank(instId)) {
                        user.setInstId(instId);;
                    }
                }
                queryWrapper.likeRight(CupsBrandFeeReportInfo::getInstId, instId);
                if (StringUtils.isNotBlank(entity.getInstId())) {
                    queryWrapper.eq(CupsBrandFeeReportInfo::getInstId, entity.getInstId());
                }
                Page<CupsBrandFeeReportInfo> page = new Page<>();
                IPage<CupsBrandFeeReportInfo> retPage = this.cupsBrandFeeReportMapper.findByPage(page,entity);
                SortUtil.handlePageSort(request, page, true);
                return retPage;
        } catch (Exception e) {
                log.error("查询失败", e);
                return null;
        }
    }

    @Override
    public IPage<CupsBrandFeeReportInfo> findOrgChargeDetail(QueryRequest request, CupsBrandFeeReportInfo cupsBrandFeeReportInfo) {
        try {
            Page<CupsBrandFeeReportInfo> page = new Page<>();
            SortUtil.handlePageSort(request, page, true);
            return this.page(page);
        } catch (Exception e) {
            log.error("获取角色信息失败", e);
            return null;
        }

    }

}
